package com.macro.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.common.api.CommonPageResult;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.convert.PmsProductCategoryAttrCategoryConvert;
import com.macro.mall.dto.PmsProductAttributeSetParam;
import com.macro.mall.dto.PmsProductAttributeSetItem;
import com.macro.mall.model.pms.PmsProductAttributeSetDO;
import com.macro.mall.service.PmsProductAttributeSetService;
import com.macro.mall.service.PmsProductCategoryAttributeSetMappingDomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性分类管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmsProductAttributeSetController")
@Tag(name = "PmsProductAttributeSetController", description = "商品属性分类管理")
@RequestMapping("/sup/attributeSet")
public class PmsProductAttributeSetController {
    @Autowired
    private PmsProductAttributeSetService productAttributeSetService;

    @Autowired
    private PmsProductCategoryAttributeSetMappingDomainService productCategoryAttrCategoryMappingDomainService;

    @Autowired
    private PmsProductCategoryAttrCategoryConvert pmsProductCategoryAttrCategoryConvert;

    @ApiOperation("添加商品属性分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestParam String name) {
        int count = productAttributeSetService.create(name);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品属性分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestParam String name) {
        int count = productAttributeSetService.update(id, name);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除单个商品属性分类")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = productAttributeSetService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("获取单个商品属性分类信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProductAttributeSetDO> getItem(@PathVariable Long id) {
        PmsProductAttributeSetDO productAttributeCategory = productAttributeSetService.getItem(id);
        return CommonResult.success(productAttributeCategory);
    }

    @ApiOperation("分页获取所有商品属性分类")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPageResult<PmsProductAttributeSetDO>> getList(
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNum
    ) {
        List<PmsProductAttributeSetDO> productAttributeCategoryList =  productAttributeSetService.getList(pageSize, pageNum);
        return CommonResult.success(CommonPageResult.restPage(productAttributeCategoryList));
    }

    @ApiOperation("分页获取所有商品属性分类")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Page<PmsProductAttributeSetDO>> getList(@RequestBody PmsProductAttributeSetParam param) {

        LambdaQueryWrapper<PmsProductAttributeSetDO> wrapper = Wrappers.lambdaQuery();
        if (param.getName() != null) {
            wrapper.like(PmsProductAttributeSetDO::getName, param.getName());
        }
        Page<PmsProductAttributeSetDO> page = new Page<>(param.getPageNum(), param.getPageSize());
        Page<PmsProductAttributeSetDO> pageResult =  productAttributeSetService.page(page, wrapper);
        return CommonResult.success(pageResult);
    }

    @ApiOperation("获取所有商品属性分类及其下属性")
    @RequestMapping(value = "/list/withAttr", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductAttributeSetItem>> getListWithAttr() {
        List<PmsProductAttributeSetItem> productAttributeCategoryResultList = productAttributeSetService.getListWithAttr();
        return CommonResult.success(productAttributeCategoryResultList);
    }
}
